package br.pucrio.inf.les.SPLAnalyser.impl.query;

import java.util.ArrayList;
import java.util.List;

import br.pucrio.inf.les.SPLAnalyser.core.Query;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Asset;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Feature;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.ImpactAnalysisModel;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactanalysismodel.Mapping;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ImpactVisualizerModel;
import br.pucrio.inf.les.SPLAnalyser.core.models.impactvisualizermodel.ResultSet;
import br.pucrio.inf.les.SPLAnalyser.core.models.util.ImpactAnalysisModelContent;
import br.pucrio.inf.les.SPLAnalyser.core.models.util.ImpactVisualizerModelHandler;
import br.pucrio.inf.les.SPLAnalyser.impl.query.models.RemoveFeatureQueryModel;


public class ImpactOfRemoveFeature implements Query {
	private ImpactAnalysisModelContent analysisModelContent;		
	private String featureName;
		
	public ImpactOfRemoveFeature(String featureName){
		this.featureName = featureName;
	}
	
	public RemoveFeatureQueryModel runQuery(ImpactAnalysisModel analysisModel) {
		analysisModelContent = new ImpactAnalysisModelContent(analysisModel);
		Feature feature = analysisModelContent.findFeature(featureName);
		
		List<Asset> exclusiveAssets = new ArrayList<Asset>();
		List<Asset> dependentAssets = new ArrayList<Asset>();
		List<Asset> sharedAssets = new ArrayList<Asset>();
		List<Feature> affectedFeatures = new ArrayList<Feature>();
		
		List<Mapping> mappings = analysisModelContent.findMappingOf(feature);
		for(Mapping map : mappings){
			if(map.getFeatures().size() == 1){
				exclusiveAssets.add(map.getAssets());
			}else if (map.getFeatures().size() > 1){
				sharedAssets.add(map.getAssets());
				affectedFeatures.addAll(map.getFeatures());
			}
		}
		
		for(Asset asset : exclusiveAssets){
			dependentAssets.addAll( analysisModelContent.getAssetsDependenciasOf(asset));
		}
		
		for(Asset asset : dependentAssets){
			Mapping map = analysisModelContent.findMappingOf(asset);
			affectedFeatures.addAll(map.getFeatures());			
		}
		RemoveFeatureQueryModel result = new RemoveFeatureQueryModel(exclusiveAssets, 
				dependentAssets, sharedAssets, affectedFeatures);
		
		
		return result;	
	}

	public String getName() {
		return this.getClass().getSimpleName() + "_" + featureName;
	}
	
	private ImpactVisualizerModel createQueryModel(List<Asset> exclusiveAssets, List<Asset> dependentAssets, List<Asset> sharedAssets, List<Feature> affectedFeatures ){
		ImpactVisualizerModelHandler queryModelContent = new ImpactVisualizerModelHandler(getName());
		
		ResultSet exclusiveAssetsSet = queryModelContent.addResultSet("Exclusive Assets");
		queryModelContent.addAssetResult(exclusiveAssetsSet, exclusiveAssets);
		
		ResultSet dependentAssetsSet = queryModelContent.addResultSet("Dependent Assets");
		queryModelContent.addAssetResult(dependentAssetsSet, dependentAssets);
		
		ResultSet sharedAssetsSet = queryModelContent.addResultSet("Shared Assets");
		queryModelContent.addAssetResult(sharedAssetsSet, sharedAssets);
		
		ResultSet affectedFeaturesSet = queryModelContent.addResultSet("Affected Features");
		queryModelContent.addFeatureResult(affectedFeaturesSet, affectedFeatures);
		
		return queryModelContent.getImpactVisualizerModel()	;
		
	}
}
